package org.devops

//deployment module
/*
deployment 模板文件
service    模板文件 
dockerfile 模板文件
nginx_configs 模板文件 
*/
def deployment(proName,image,projectName,port=80){
    deployment = """
                    kind: Deployment
apiVersion: apps/v1
metadata:
  name: ${proName}
  namespace: ${projectName}
  labels:
    app: ${proName}
    app.kubernetes.io/name: ${projectName}
    app.kubernetes.io/version: v1
    version: v1
  annotations:
    deployment.kubernetes.io/revision: '4'
    kubesphere.io/creator: admin
    servicemesh.kubesphere.io/enabled: 'true'
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ${proName}
      app.kubernetes.io/name: ${projectName}
      app.kubernetes.io/version: v1
      version: v1
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: ${proName}
        app.kubernetes.io/name: ${projectName}
        app.kubernetes.io/version: v1
        version: v1
      annotations:
        kubesphere.io/containerSecrets: '{"container-tdacgi":"harbor"}'
        kubesphere.io/restartedAt: '2021-04-14T08:35:53.792Z'
        sidecar.istio.io/inject: 'true'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
      containers:
        - name: container-tdacgi
          image: "${image}"
          ports:
            - name: tcp-${port}
              containerPort: ${port}
              protocol: TCP
          env:
            - name: NACOS_HOST
              value: nacos.${projectName}.svc.cluster.local
            - name: NACOS_PORT
              value: '8848'
            - name: NACOS_USER
              value: nacos
            - name: NACOS_PASSWD
              value: nacos
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: Always
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      imagePullSecrets:
        - name: harbor
      affinity: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
                    """
                return deployment.trim()
}

//Services module
def Services(proName,projectName,port=80){
    Services = """
                    kind: Service
apiVersion: v1
metadata:
  name: ${proName}
  namespace: ${projectName}
  labels:
    app: ${proName}
    app.kubernetes.io/name: ${projectName}
    app.kubernetes.io/version: v1
    version: v1
  annotations:
    kubesphere.io/creator: admin
    kubesphere.io/serviceType: statelessservice
    servicemesh.kubesphere.io/enabled: 'true'
spec:
  ports:
    - name: tcp-${port}
      protocol: TCP
      port: ${port}
      targetPort: ${port}
  selector:
    app: ${proName}
    app.kubernetes.io/name: ${projectName}
    app.kubernetes.io/version: v1
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
                    """
  return Services.trim()
}

//nginx 配置文件
def nginx_conf() {
  tmp = '''
    #user  nobody;
worker_processes  auto;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile   on;
    tcp_nopush on;

    keepalive_timeout 65;
    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;

    limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

    server_tokens off;
    access_log off;

    server {
        listen 80;
        server_name _;
#########################################################################
        set $host_ip "172.16.1.122";
        set $port    "30500";
#########################################################################
        location /auth-service/ {
            proxy_pass        http://172.16.1.122:32091/auth-service/;       
        }

        location /tenant-system-service/ {
            proxy_pass        http://172.16.1.122:32091/tenant-system-service/;
        }

        location /contract-service/ {
            proxy_pass        http://172.16.1.122:32091/contract-service/;
            proxy_buffer_size 1024k;
            proxy_buffers 16 1024k;
            proxy_busy_buffers_size 2048k;
        }

        location /app-seal-service/ {
            proxy_pass        http://172.16.1.122:32091/app-seal-service/;
        }

        location /esign-attachmen/ {
            proxy_pass        http://172.16.1.122:32091/esign-attachmen/;
        }

        location /file-service/ {
            proxy_pass        http://172.16.1.122:32091/file-service/;
        }
        location /cert-service/ {
            proxy_pass        http://172.16.1.122:32091/cert-service/;
        }
        location /template-service/ {
            proxy_pass        http://172.16.1.122:32091/template-service/;
        }
        location /scan-code-service/ {
            proxy_pass        http://172.16.1.122:32091/scan-code-service/;
        }

        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size    1000m;

        location / {
            root html;
            index index.html;
        }

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
            root html;
        }

    }
}
  '''
  return tmp.trim()
}

//运营平台nginx 配置文件
def yun_ying_nginx_conf() {
  tmp = '''
    #user  nobody;
worker_processes  auto;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile   on;
    tcp_nopush on;

    keepalive_timeout 65;
    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;

    limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

    server_tokens off;
    access_log off;

    server {
        listen 80;
        server_name _;
        location /api/ {
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass        http://172.16.1.122:32015;
        }
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size    1000m;

        location / {
            root html;
            try_files $uri $uri/ /index.html;
            index index.html;
        }

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
            root html;
        }

    }
}
  '''
  return tmp.trim()
}

//盘古前台nginx配置
def pangu_nginx_conf() {
  tmp = '''
    #user  nobody;
worker_processes  auto;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile   on;
    tcp_nopush on;

    keepalive_timeout 65;
    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;

    limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

    server_tokens off;
    access_log off;

    server {
        listen 80;
        server_name _;
        location /api/ {
            proxy_pass        http://172.16.1.122:32091/api/;       
        }
        location /file/ {
            proxy_pass        http://172.16.1.122:32091/file/;       
        }
        location /auth-service/ {
            proxy_pass        http://172.16.1.122:32091/auth-service/;       
        }
        location /platform-system-service/ {
            proxy_pass        http://172.16.1.122:32091/platform-system-service/;       
        }
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size    1000m;

        location / {
            root html;
            index index.html;
        }

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
            root html;
        }

    }
}
  '''
  return tmp.trim()
}

//判断是否需要在当前位置编译,返回工作目录
def pronames(proname,workdir) {
    if (proname == './') {
        return workdir.trim()
    }else{
        //规范proName名称
        proname = proname.split('/')[-1]
        return proname.trim()
    } 
}

//dockfile 模板文件
def dockfile(port,startcmd) {
    fileinfo = """
    FROM harbor.ebcall.com/library/jdk:1.8
ADD *.jar app.jar
EXPOSE ${port}
ENV LANG en_US.UTF-8
ENTRYPOINT ${startcmd}
"""
return fileinfo.trim()
}

//前端dockfile 模板文件
def dockfile_before(path="dist") {
    fileinfo = """
    FROM harbor.ebcall.com/library/nginx:1.8
ADD ${path} /usr/local/nginx/html
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
ENTRYPOINT ["/usr/local/nginx/sbin/nginx"]
CMD ["-g", "daemon off;"]
"""
return fileinfo.trim()
}

//nginx通用模板文件(不含server)
def nginx_configs(server_conf) {
conf = """worker_processes  auto;
worker_rlimit_nofile 51200;
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    sendfile   on;
    tcp_nopush on;
    keepalive_timeout 65;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
	  fastcgi_intercept_errors on;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    limit_conn_zone \$binary_remote_addr zone=perip:10m;
	  limit_conn_zone \$server_name zone=perserver:10m;
    server_tokens off;
    access_log off;
    ${server_conf}
    }"""
    return conf.trim()
}


